<?php
namespace app\issue\controller;
use app\common\controller\IssueBase;

class Admin extends IssueBase {

    public function _initialize()
    {
        parent::_initialize();
    }

    public function user() {
        $auth_group = sp_get_auth_menu_3("20071000");
        $this->assign("menus", $auth_group);
        echo $this->fetch();
    }

    public function ajaxUser() {
        $name = input('name/s');
        $account = input('account/s');
        $draw = input("draw",0) + 1;//请求时间

        $where = array();
        if (!empty($name)) {
            $where['u.name'] = array("like", "%{$name}%");
        }
        if (!empty($account)) {
            $where['u.account'] = array("like", "%{$account}%");
        }
        $total = db("operation_user")->alias("u")->where($where)->count(1);
        $list = db("operation_user")->alias("u")
            ->join("m_operation_user_group ug", "u.group_id = ug.id", "left")
            ->field("u.*, ug.name as group_name")
            ->where($where)
            ->order($this->get_order())
            ->limit($this->get_limit())
            ->select();

        $data = array('pageData' => $list, 'total' => $total, "draw" => $draw);
        $this->ajax($data);
    }

    public function userAdd()
    {
        if (request()->isPost()) {
            $name = input("name/s");
            $account = input("account/s");
            $password = input("password/s");
            $group_id = input("group_id/d");
            $mobile = input("mobile/s");
            if (empty($name)) {
                $this->ajax_error("昵称不能为空");
            }
            if (empty($account)) {
                $this->ajax_error("账号不能为空");
            }
            if (empty($password)) {
                $this->ajax_error("密码不能为空");
            }
            if (empty($group_id)) {
                $this->ajax_error("分组不能为空");
            }
            if (empty($mobile)) {
                $this->ajax_error("手机号不能为空");
            }
            if (!preg_match("/^1[34578]\d{9}$/", $mobile)) {
               $this->ajax_error("手机号格式不正确");
            }
            $group = db("operation_user_group")->find($group_id);
            if (empty($group)) {
                $this->ajax_error("分组不存在");
            }
            $is_exist = db("operation_user")->where(array("account" => $account))->find();
            if (!empty($is_exist)) {
                $this->ajax_error("账号已存在，请重新输入");
            }
            $add_res = db("operation_user")->insert(array("name" => $name, "account" => $account, "password" => md5($password), "group_id" => $group_id, "mobile" => $mobile));
            if (empty($add_res)) {
                $this->ajax_error("添加失败，请重试");
            }
            $this->ajax_success("添加成功");
        } else {
            $groups = db("operation_user_group")->select();
            $this->assign("groups", $groups);
            echo $this->fetch();
        }
    }

    public function userEdit() {
        $id = input("id/d");
        $name = input("name/s");
        if (empty($id)) {
            $this->ajax_error("参数错误");
        }
        $user = db("operation_user")->find($id);
        if (empty($user)) {
            $this->ajax_error("参数错误");
        }
        if (!empty($name)) {

            $account = input("account/s");
            $password = input("password/s");
            $group_id = input("group_id/d");
            $mobile = input("mobile/s");
            if (empty($name)) {
                $this->ajax_error("昵称不能为空");
            }
            if (empty($account)) {
                $this->ajax_error("账号不能为空");
            }

            if (empty($group_id)) {
                $this->ajax_error("分组不能为空");
            }
            if (empty($mobile)) {
                $this->ajax_error("手机号不能为空");
            }
            $group = db("operation_user_group")->find($group_id);
            if (empty($group)) {
                $this->ajax_error("分组不存在");
            }
            $is_exist = db("operation_user")->where(array("account" => $account, "id" => array("neq", $id)))->find();
            if (!empty($is_exist)) {
                $this->ajax_error("账号已存在，请重新输入");
            }
            $update_arr = array("name" => $name, "account" => $account, "group_id" => $group_id, "mobile" => $mobile);
            if (!empty($password)) {
                $update_arr["password"] = md5($password);
            }
            $add_res = db("operation_user")->where(array("id" => $id))->update($update_arr);
            if ($add_res === false) {
                $this->ajax_error("修改失败，请重试");
            }
            $this->ajax_success("添加成功");
        } else {
            $groups = db("operation_user_group")->select();
            $this->assign("user", $user);
            $this->assign("groups", $groups);
            echo $this->fetch();
        }
    }


    public function userDelete()
    {
        $id = input("id/d");
        if (empty($id)) {
            $this->ajax_error("参数错误");
        }
        $user = db("operation_user")->find($id);
        if (empty($user)) {
            $this->ajax_error("用户不存在");
        }
        $del_res = db("operation_user")->delete($id);
        if (empty($del_res)) {
            $this->ajax_error("删除失败");
        }
        $this->ajax_success("删除成功");
    }
}